package com.estun.backroundManagementService.modules.material.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.material.model.Material;
import com.estun.backroundManagementService.modules.material.service.MaterialService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;


@RestController
@RequestMapping("/material")
@RequiredArgsConstructor
public class MaterialController {

    private final MaterialService materialService;


    //分页查询物料信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<Material> pageRequest) {
        Page<Material> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<Material> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<Material> page1 = materialService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    //调用工厂mes同步物料主数据
    @PostMapping("/sync")
    @Log(module = "物料主数据 工厂ems同步", type = LogConstants.INSERT, desc = "物料主数据 工厂ems同步")
    public BaseResponse sync(@RequestBody Object object) {
        return materialService.sync();
    }

    // 根据ID获取物料信息
    @GetMapping("/{id}")
    public BaseResponse getMaterialById(@PathVariable String id) {
        return BaseResponse.success(materialService.getById(id));
    }

    // 添加物料信息
    @PostMapping("/add")
    @Log(module = "物料主数据 工厂ems同步", type = LogConstants.INSERT, desc = "物料主数据 工厂ems同步")
    public BaseResponse addMaterial(@RequestBody Material material) {
        return BaseResponse.success(materialService.save(material));
    }

    // 更新物料信息
    @PutMapping("/update")
    @Log(module = "物料主数据 更新", type = LogConstants.UPDATE, desc = "物料主数据 更新")
    public BaseResponse updateMaterial(@RequestBody Material material) {
        return BaseResponse.success(materialService.updateById(material));
    }

    // 删除物料信息
    @DeleteMapping("/delete/{id}")
    @Log(module = "物料主数据 删除", type = LogConstants.DELETE, desc = "物料主数据 删除")
    public BaseResponse deleteMaterial(@PathVariable String id) {
        return BaseResponse.success(materialService.removeById(id));
    }


    @ApiOperation("查询所有物料")
    @GetMapping(value = "/list")
    @ResponseBody
    public BaseResponse list() {
        return BaseResponse.success(materialService.list());
    }


    /**
     * 物料绑定
     *
     * @param assemblyMaterialSn 产品码
     * @param snNumber           物料编码
     * @param materialType       物料类型
     *                           "上油路板"
     *                           "下油路板"
     *                           "主压力电磁阀"
     *                           "过冷保护电磁阀"
     *                           "离合器电磁阀"
     */
    @ApiOperation("查询所有物料")
    @GetMapping(value = "/bind")
    @ResponseBody
    public BaseResponse bind(@Param("assemblyMaterialSn") String assemblyMaterialSn,
                             @Param("materialType") String materialType,
                             @Param("snNumber") String snNumber,
                             @Param("opCode") String opCode) {
        return materialService.bind(assemblyMaterialSn, materialType, snNumber, opCode);
    }
    @ApiOperation("MES调用AGV自动上料")
    @GetMapping(value = "/agv")
    @ResponseBody
    public void agv(@Param("materialCode") String materialCode, @Param("toPointCode") String toPointCode  ) {
        materialService.agv(materialCode,toPointCode);
    }

}
